﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerBienLaiThuChiTietDao : IBienLaiThuChiTietDao
    {
        public SqlServerBienLaiThuChiTietDao()
        {
            SQLUtility.TableName = "BienLaiThuChiTiet";
        }

        //Trả về đối tượng báo ăn
        private BienLaiThuChiTiet MakeBienLaiThuChiTiet(DataRow row)
        {
            Guid _id = Guid.Parse(row["DetailThuID"].ToString());
            Guid _dinhMucThuID = Guid.Parse(row["DinhMucThuID"].ToString());
            decimal _dongia = Convert.ToDecimal(row["Dongia"].ToString());
            decimal _soluong = Convert.ToDecimal(row["Soluong"].ToString());
            decimal _thanhtien = Convert.ToDecimal(row["Thanhtien"].ToString());
            bool _nopdu = Convert.ToBoolean(row["Nopdu"].ToString());
            decimal _dinhmucThuThucTe = Convert.ToDecimal(row["DinhmucThuThucTe"].ToString());
            decimal _conphaiThu = Convert.ToDecimal(row["ConphaiThu"].ToString());
            long _bienLaiThuID = long.Parse(row["BienLaiThuID"].ToString());
            return new BienLaiThuChiTiet(_id, _dinhMucThuID, _dongia, _soluong, _thanhtien, _nopdu, _dinhmucThuThucTe, _conphaiThu, _bienLaiThuID);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<BienLaiThuChiTiet> MakeBBienLaiThuChiTiets(DataTable dt)
        {
            IList<BienLaiThuChiTiet> list = new List<BienLaiThuChiTiet>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeBienLaiThuChiTiet(row));
            }
            return list;
        }

        public IList<Business_Objects.BienLaiThuChiTiet> GetBienLaiThuChiTiet()
        {
            SQLUtility.TableName = "BienLaiThuChiTiet";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeBBienLaiThuChiTiets(dt);
        }

        public Business_Objects.BienLaiThuChiTiet GetBienLaiThuChiTietByID(string BienLaiThuChiTietID)
        {
            SQLUtility.TableName = "BienLaiThuChiTiet";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@DetailThuID", BienLaiThuChiTietID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeBienLaiThuChiTiet(row);
            }
        }

        public void InsertBienLaiThuChiTiet(Business_Objects.BienLaiThuChiTiet BienLaiThuChiTiet)
        {
            SQLUtility.TableName = "BienLaiThuChiTiet";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(BienLaiThuChiTiet), true);
        }

        public int UpdateBienLaiThuChiTiet(Business_Objects.BienLaiThuChiTiet BienLaiThuChiTiet)
        {
            SQLUtility.TableName = "BienLaiThuChiTiet";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(BienLaiThuChiTiet)); }
            catch { return -1; }
        }

        public int DeleteBienLaiThuChiTiet(Business_Objects.BienLaiThuChiTiet BienLaiThuChiTiet)
        {
            SQLUtility.TableName = "BienLaiThuChiTiet";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(BienLaiThuChiTiet)); }
            catch { return -1; }
        }

        private DbParameter[] Take(BienLaiThuChiTiet BienLaiThuChiTiet)
        {
            DbParameter[] parameters = new DbParameter[9];
            parameters[0] = new SqlParameter("@DetailThuID", BienLaiThuChiTiet.Id);
            parameters[1] = new SqlParameter("@DinhMucThuID", BienLaiThuChiTiet.DinhMucThuID);
            parameters[2] = new SqlParameter("@Dongia", BienLaiThuChiTiet.Dongia);
            parameters[3] = new SqlParameter("@Soluong", BienLaiThuChiTiet.Soluong);
            parameters[4] = new SqlParameter("@Thanhtien", BienLaiThuChiTiet.Thanhtien);
            parameters[5] = new SqlParameter("@Nopdu", BienLaiThuChiTiet.Nopdu);
            parameters[6] = new SqlParameter("@DinhmucThuThucTe", BienLaiThuChiTiet.DinhmucThuThucTe);
            parameters[7] = new SqlParameter("@ConphaiThu", BienLaiThuChiTiet.ConphaiThu);
            parameters[8] = new SqlParameter("@BienLaiThuID", BienLaiThuChiTiet.BienLaiThuID);

            return parameters;
        }


        public List<BienLaiThuChiTiet> GetBienLaiThuChiTietByBienLaiThuID(int BienLaiThuID)
        {
            SQLUtility.TableName = "BienLaiThuChiTiet";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@BienLaiThuID", BienLaiThuID);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectBienLaiThuChiTietsByAndBienLaiThuID", parameters);
            if (dt == null) { return null; }
            else
            {
               
                return MakeBBienLaiThuChiTiets(dt).ToList();
              
            }
        }
    }
}
